package com.synjones.handsetS8;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;

import com.synjones.cominterface.DataTransChannel;
import com.synjones.cominterface.ReaderInterface;
import com.synjones.handsetS8L.MyUtils;
import com.synjones.idcard.IDcardReader;
import com.synjones.multireaderlib.MultiReader;

import java.io.File;

import android_serialport_api.SerialPort;

/**
 * Created by zhaodianbo on 2016-4-12.
 */
public class S8Interface implements ReaderInterface {
    DataTransChannel dataTransChannel;
    SerialPort serialPort;
   // Activity activity;
    boolean isOpen=false;
    String path="S8手持机";
    String samvID;
    public S8Interface(){
        dataTransChannel=new DataTransChannel();
    }
    @Override
    public void setActivityAndHandler(Activity act, Handler mHandler) {
        //this.activity=act;
    }
    Context context;
    @Override
    public void setContext(Context context) {
        this.context=context;
    }

    @Override
    public void release() {
        close();
        WtWdPowerUtils.closeAllPower(context);
        serialPort=null;
    }

    @Override
    public boolean open() {
        if(isOpen()) {
            close();
            MyUtils.sleep(500);
        }
        //S8PowerControl.getInstance().setIDReaderPowerOn();
        WtWdPowerUtils.setIDPower(context);
        MyUtils.sleep(500);//使模块上电完成
        /* Open the serial port */
        try {
            serialPort = new SerialPort(new File("/dev/ttyMT0"), 115200, 0);
            dataTransChannel.setInOutStream(serialPort.getInputStream(),serialPort.getOutputStream());
            dataTransChannel.open();
            long t=System.currentTimeMillis();
            while(System.currentTimeMillis()-t<1000){
                if(canReadSamv())
                    break;
                MyUtils.sleep(100);
            }
            isOpen=true;
            //isOpen=!samvID.isEmpty();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return isOpen;
    }

    public boolean canReadSamv(){
        MultiReader multiReader= MultiReader.getReader();
        multiReader.setDataTransInterface(dataTransChannel);
        IDcardReader iDcardReader=new IDcardReader(multiReader);
        samvID=iDcardReader.getSamvIDStr();
        return !samvID.isEmpty();
    }

    @Override
    public void close() {
       if(serialPort!=null) {
           try {
               serialPort.close();
           }catch (Exception e){}
       }
        serialPort=null;
        isOpen = false;
        if(dataTransChannel!=null){
            try{
                dataTransChannel.close();
            }catch (Exception e){}
        }
        //S8PowerControl.getInstance().setIDReaderPowerOff();
        WtWdPowerUtils.closeIDPower(context);
    }

    @Override
    public boolean isOpen() {
        return isOpen;
    }

    @Override
    public DataTransChannel getDataTransChannel() {
        return dataTransChannel;
    }

    @Override
    public String getDevName() {
        return path;
    }

    @Override
    public String getSamvID() {
        return samvID;
    }
}
